// pages/my/myassessment/myassessment.js
Page({
  data: {
    assessmentData: [],
    pageNo: 1,
    pageSize: 6,
    userId: '',
    isLoading: false,
    hasMoreData: true
  },

  onLoad(options) {
    const userId = wx.getStorageSync('userId');
    this.setData({ userId });
    this.loadAssessments();
  },

  loadAssessments() {
    if (this.data.isLoading || !this.data.hasMoreData) return;

    this.setData({ isLoading: true });

    wx.request({
      url: `http://localhost:8080/api/my/assessment/getAssessments`,
      method: 'GET',
      data: {
        userId: this.data.userId,
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize
      },
      success: (res) => {

        console.log(res);

        if (res.statusCode === 200 && res.data.assessments) {
          const newData = res.data.assessments;
          if (newData.length > 0) {
            this.setData({
              assessmentData: [...this.data.assessmentData, ...newData],
              pageNo: this.data.pageNo + 1
            });
          } else {
            this.setData({ hasMoreData: false });
          }
        } else {
          console.error('Failed to load assessments:', res);
          wx.showToast({
            title: '加载失败，请重试',
            icon: 'none'
          });
        }
      },
      fail: (error) => {
        console.error('Request failed:', error);
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({ isLoading: false });
        wx.stopPullDownRefresh();
      }
    });
  },

  onReachBottom() {
    this.loadAssessments();
  },

  onPullDownRefresh() {
    this.setData({
      assessmentData: [],
      pageNo: 1,
      hasMoreData: true
    });
    this.loadAssessments();
  }
});